# Holds an 'entry' about a URL that SFX can target. 

package SfxUrlData;

use strict;

sub new {
	my $proto = shift;
	my ($urlArg, $displayNameArg, $targetArg, $serviceTypeArg) = @_;
	my $class = ref($proto) || $proto;
	my $self  = {};
	bless ($self, $class);

	$self->url( $urlArg );
	$self->displayName( $displayNameArg );
	$self->targetName( $targetArg );
	$self->serviceType( $serviceTypeArg );
	
	return $self;
}

sub url {
	my $self = shift;
	my $arg = shift;
	if ( $arg ) {
		$self->{'URL'} = $arg;
		$self->{'URI_OBJ'} = URI->new( $arg );
	}	
	return $self->{'URL'};
}
sub uriObj {
	my $self = shift;
	return $self->{'URI_OBJ'};
}

sub host {
	my $self = shift;
	
	return $self->uriObj()->host();
}
sub canonicalDomain {
	my $self = shift;
	# Domain suitable for using in EZProxyConfig. 
	# Remove the first element of the hostname, 
	# we want to unique on n-1 element hostname.
	my $host = $self->host();
	
	# But not for numeric IPs! Sad that we have any in there anyway. sigh. 
	if ( $host =~ /^(\d|\.)+$/ ) {
		return $host;
	}
	
	my @elements = split( /\./, $host);
	if ( $#elements > 1 ) {
		shift @elements;
		$host = join('.', @elements);
	}
	return $host;
}

# Internal name of target.
sub targetName {
	my $self = shift;
	my $arg = shift;
	if ( $arg ) {
		$self->{'TARGET_NAME'} = $arg;
	}
	
	return $self->{'TARGET_NAME'};
}

# "getFullTxt" etc. 
sub serviceType {
	my $self = shift;
	my $arg = shift;
	if ( $arg ) {
		$self->{'SERVICE_TYPE'} = $arg;
	}
	
	return $self->{'SERVICE_TYPE'};
}

sub serviceInternalID {
	my $self = shift;
	my $arg = shift;
	if ( $arg ) {
		$self->{'SERVICE_INTERNAL_ID'} = $arg;
	}
	
	return $self->{'SERVICE_INTERNAL_ID'};
}
	

# Combine target internal name and service type to make a unique identifier
# for the target service. 
sub targetServiceKey() {
	my $self = shift;
	return $self->targetName() . '-' . $self->serviceType();
}

sub displayName {
	my $self = shift;
	my $arg = shift;
	if ( $arg ) {
		$self->{'DISPLAY_NAME'} = $arg;
	}
	
	my $displayName = $self->{'DISPLAY_NAME'};
	return ($displayName ? $displayName : $self->targetName());
}

# Where did we get this from? A target, a target service, something else. 
# Really ought to be an enumerated type, not just a free string, but
# I dn't know how to do that in perl. 
sub sourceType() {
	my $self = shift;
	my $arg = shift;
	
	if ( $arg ) {
		$self->{'SOURCE_TYPE'} = $arg;
	}
	
	return $self->{'SOURCE_TYPE'};
}

sub defaultAvailability {
	my $self = shift;
	my $arg = shift;
	if ($arg) {
		$self->{'DEFAULT_AVAILABILITY'} = $arg;
	}
	return $self->{'DEFAULT_AVAILABILITY'};
}


return 1;
